﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TradeForm.Properties;

namespace TradeForm
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }


        private void FrmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter && this.ActiveControl.GetType() != System.Type.GetType("System.Windows.Forms.Button"))
            {
                e.KeyChar = (char)Keys.Tab;
                // e.Handled = false;
                this.SelectNextControl(this.ActiveControl, true, true, false, true);
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            EncryptOperate encrypt = new EncryptOperate();
            string str = encrypt.EncryptIt(this.txtPwd.Text);
            if (this.chkRemberPwd.Checked)
            {
                Settings.Default.UserID = this.txtUserID.Text.ToLower();
                Settings.Default.Pwd = encrypt.EncryptIt(this.txtPwd.Text);
                Settings.Default.Rember = true;
                Settings.Default.Save();
            }
            else
            {
                Settings.Default.UserID = "";
                Settings.Default.Pwd = "";
                Settings.Default.Rember = false;
            }
            if (Utility.PwdIsCorrect(this.txtUserID.Text.ToLower(), this.txtPwd.Text))
            {
                Utility.UserID = this.txtUserID.Text.ToLower();
                this.DialogResult = DialogResult.OK;
                return;
            }
            else
            {
                this.lblMsg.Text = "密码不正确";
                this.DialogResult = DialogResult.None;
            }

        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            EncryptOperate encrypt = new EncryptOperate();
            this.lblMsg.Text = string.Empty;
            try
            {
                bool IsRemberPwd = Settings.Default.Rember;
                this.chkRemberPwd.Checked = IsRemberPwd;
                if (IsRemberPwd)
                {
                    this.txtPwd.Text = encrypt.DecryptIt(Settings.Default.Pwd);
                    this.txtUserID.Text = Settings.Default.UserID;
                }
            }
            catch
            {
                this.chkRemberPwd.Checked = false;
            }
        }

        private void txtPwd_Enter(object sender, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
        }

        private void txtUserID_Enter(object sender, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
        }


    }
}
